package main

import (
	"fmt"
	"io"
	"os"
	"strings"
)

func readIO(reader io.Reader, num int) ([]byte, error) {

	//创建一个缓冲流的切片（make方法）
	p := make([]byte, num)

	n, err := reader.Read(p)

	if n > 0 {
		return p[:n], nil
	}

	return p, err
}

//从字符串读取
func readIOFromString() {
	data, _ := readIO(strings.NewReader("read from string"), 1200)

	fmt.Println(data)
}

//从控制输入
func readIOFromStdin() {
	fmt.Println("please input something:")

	data, _ := readIO(os.Stdin, 9)

	fmt.Println(string(data))
}

//从文件中读取
func readIOFromFile() {
	//	f, _ := os.OpenFile("main.go", os.O_RDONLY, 0)
	f, _ := os.Open("main.go")

	data, _ := readIO(f, 11)

	defer f.Close()

	fmt.Println(string(data))
}

func main() {
	//	readIOFromString()
	//	readIOFromStdin()

	readIOFromFile()
}
